const Resource = require('../model').Resource

exports.saveFileInTeamBySb = function(file, team, upload) {
  return Resource.create({
    name: file.originalname,
    kb_size: file.size,
    ext: file.originalname.replace(/.*\./, '').toLowerCase(),
    savedFileName: file.filename,
    team,
    upload
  })
}

exports.getAllResourcesInTeam = function(team, nSkip, nLimit) {
  return Resource.find({ team, delete: false })
    .sort({ created_at: -1 }).skip(nSkip).limit(nLimit)
    .select('-delete -team -savedFileName -upload')
}

exports.existResourceInTeam = function(_id, team) {
  return Resource.findOneAndUpdate({ _id, team }, {
    $inc: { download_times: 1 }
  }, { new: true })
}

exports.deleteResourceInTeam = function(_id, team) {
  return Resource.findOneAndUpdate({ _id, team }, { delete: true })
}
